package com.example.hospital.api.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.example.hospital.api.common.PageUtils;
import com.example.hospital.api.db.dao.DrugNumDao;
import com.example.hospital.api.db.dao.DrugOperateDao;
import com.example.hospital.api.db.pojo.DrugOperateEntity;
import com.example.hospital.api.service.DrugNumService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class DrugNumServiceImpl implements DrugNumService {

    @Resource
    private DrugNumDao drugNumDao;

    @Resource
    private DrugOperateDao drugOperateDao;

    @Override
    public PageUtils searchNumByPage(Map param) {
        ArrayList<HashMap> list = null;
        long count = drugNumDao.searchNumCount(param);
        if (count > 0) {
            list = drugNumDao.searchNumByPage(param);
        } else {
            list = new ArrayList<>();
        }
        int page = MapUtil.getInt(param, "page");
        int length = MapUtil.getInt(param, "length");
        PageUtils pageUtils = new PageUtils(list, count, page, length);
        return pageUtils;
    }

    @Override
    public HashMap searchById(int id) {
        HashMap map = drugNumDao.searchById(id);
        return map;
    }

    @Override
    public void addNumById(Map param) {
        int id = MapUtil.getInt(param, "id");
        int n = MapUtil.getInt(param, "n");
        drugNumDao.addNumById(new HashMap() {{
            put("id", id);
            put("n", n);
        }});
        HashMap hashMap = drugNumDao.searchById(id);
        String drugid = (String) hashMap.get("drugid");
        String drugname = (String) hashMap.get("drugname");

        DrugOperateEntity drugOperateEntity=new DrugOperateEntity();
        drugOperateEntity.setDrugid(drugid);
        drugOperateEntity.setDrugname(drugname);
        drugOperateEntity.setUser("root");
        drugOperateEntity.setNum(n);
        drugOperateEntity.setStatus((byte) 2);
        drugOperateEntity.setRemark("药房采购");
        drugOperateDao.addinsert(drugOperateEntity);
    }

    @Override
    public String reduceNumById(Map param) {
        String outId=MapUtil.getStr(param,"registrationId");
        if(outId==null){
            outId="药房出药";
        }else {
            outId="挂号单出药，单号："+outId;
        }
        int id = MapUtil.getInt(param, "id");
        int n = MapUtil.getInt(param, "n");
        String flag;
        int m = drugNumDao.searchNumById(id);
        if (m < n) {
            flag = "失败";
        } else {
            drugNumDao.reduceNumById(new HashMap() {{
                put("id", id);
                put("n", n);
            }});
            HashMap hashMap = drugNumDao.searchById(id);
            String drugid = (String) hashMap.get("drugid");
            String drugname = (String) hashMap.get("drugname");

            DrugOperateEntity drugOperateEntity=new DrugOperateEntity();
            drugOperateEntity.setDrugid(drugid);
            drugOperateEntity.setDrugname(drugname);
            drugOperateEntity.setUser("root");
            drugOperateEntity.setNum(n);
            drugOperateEntity.setStatus((byte) 1);
            drugOperateEntity.setRemark(outId);
            drugOperateDao.addinsert(drugOperateEntity);
            flag = "成功";
        }
        return flag;
    }

    @Override
    public void reduce(int registrationId) {
        String outId= String.valueOf(registrationId);
        outId="挂号单出药，单号："+outId;
        String drugid[]=new String[]{"6936056125432","0894806140612"};
        String drugname[]=new String[]{"蓝岑颗粒","头孢克圬分散片"};
        int num[]=new int[]{3,2};
        for (int i = 0; i <= 1; i++) {
            DrugOperateEntity drugOperateEntity=new DrugOperateEntity();
            drugOperateEntity.setDrugid(drugid[i]);
            drugOperateEntity.setDrugname(drugname[i]);
            drugOperateEntity.setUser("root");
            drugOperateEntity.setNum(num[i]);
            drugOperateEntity.setStatus((byte) 1);
            drugOperateEntity.setRemark(outId);
            drugOperateDao.addinsert(drugOperateEntity);
        }
    }
}
